package com.hongfeng.support.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hongfeng.support.annotation.Online;
import com.hongfeng.support.common.Page;
import com.hongfeng.support.controller.common.BaseController;
import com.hongfeng.support.item.OrderItem;
import com.hongfeng.support.item.ParmItem;
import com.hongfeng.support.item.tag.FieldOperator;
import com.hongfeng.support.item.tag.OrderOperator;
import com.hongfeng.support.model.Purview;
import com.hongfeng.support.service.PurviewService;
import com.hongfeng.support.util.json.JsonUtils;

/**
 * 权限Controller
 * 
 * @author llc
 * 
 */
@Controller
@Online
public class PurviewController extends BaseController {

	@Resource
	private PurviewService purviewService;

	@RequestMapping("list")
	public void list(HttpServletRequest request, HttpServletResponse response,
			String roleName, Page<Purview> page) {
		List<OrderItem> orders = new ArrayList<OrderItem>(2);
		orders.add(new OrderItem("role.id", OrderOperator.ASC));
		orders.add(new OrderItem("menu.id", OrderOperator.ASC));
		List<ParmItem> parms = new ArrayList<ParmItem>(1);
		parms.add(new ParmItem("role.roleName", roleName, FieldOperator.LIKE));
		page = purviewService.findPageByQuery(page, Purview.class, parms,
				orders);
		String json = JsonUtils.bean2Json(page);
		log.info("查询权限结果：" + json);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("create")
	public void create(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("purview") String purviewJson) {
		log.info("创建权限：" + purviewJson);
		if (purviewJson == null) {
			this.sendErrorResponse(response, "权限信息不能为空。");
			return;
		}
		purviewService.create(purviewJson);
	}

	@RequestMapping("read")
	public void read(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("id") Long id) {
		log.info("查询权限:" + id);
		if (id == null || !id.toString().matches("\\d*")) {
			this.sendErrorResponse(response, "查询失败。");
			return;
		}
		Purview purview = purviewService.read(id);
		String json = JsonUtils.bean2Json(purview);
		log.info(json);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("update")
	public void update(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("purview") String purviewJson) {
		log.info("修改权限：" + purviewJson);
		if (purviewJson == null) {
			this.sendErrorResponse(response, "权限信息不能为空。");
			return;
		}
		purviewService.update(purviewJson);
	}

	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("ids") String ids) {
		log.info("删除权限:" + (ids != null ? ids.toString() : null));
		if (ids == null || "".equals(ids)) {
			this.sendErrorResponse(response, "请选择要删除的权限。");
			return;
		}
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			purviewService.delete(Long.parseLong(id));
		}
	}
}
